/* eslint-disable */ 
import { fetchResource } from './fetch-resource'
export const importHtml = async url => {
  const html = await fetchResource(url)
  const template = document.createElement('div')
  template.innerHTML = html
  // 获取srcipt标签
  const scripts = template.querySelectorAll('script')

  function getExternalScripts () {
    // console.log(scripts)

    return Promise.all(Array.from(scripts).map(script => {
      const src = script.getAttribute('src')
      if (!src) {
        return Promise.resolve(script.innerHTML)
      } else {
        return fetchResource(src.startsWith('http') ? src : `${url}${src}`)
      }
    }))
  }
  //   执行script的脚本
  async function execScropts () {
    const scripts = await getExternalScripts()
    console.log(scripts)

    const module = {
        exports:{}
    }
    const exports = module.exports
    scripts.forEach(code => {
      eval(code)
    })

    // console.log(module.exports)
    // console.log(window['app-vue1-app'])
    // return window['app-vue1-app']
    return module.exports
  }
  return {
    template,
    getExternalScripts,
    execScropts
  }
}
